package recordswipetime

import (
	"github.com/lzzyuhyy/framework/mysql"
	"gorm.io/gorm"
	"time"
)

type RecordSwipeTime struct {
	ID           uint      `gorm:"primarykey"`
	UserId       int       `json:"user_id" gorm:"type:int(11);not null;index:user_id;comment:用户id"`
	StartingTime time.Time `json:"session_start_time" gorm:"type:datetime;comment:开始刷题时间"`
	EndTime      time.Time `json:"session_end_time" gorm:"type:datetime;comment:结束刷题时间"`
	SumTime      uint64    `json:"sum_time" gorm:"type:bigint;comment:这次刷题的总时间,按秒计时"`
}

func (r *RecordSwipeTime) CreateRecordSwipeTime() error {
	return mysql.CreateTXClient(func(db *gorm.DB) error {
		return db.Create(&r).Error
	})
}

func (r *RecordSwipeTime) GetRecordSwipeTimeInfo() error {
	return mysql.CreateTXClient(func(db *gorm.DB) error {
		return db.Where("user_id = ?", r.UserId).Order("id desc").Limit(1).Find(&r).Error
	})
}

func (r *RecordSwipeTime) UpdateRecordSwipeTimeInfo() error {
	return mysql.CreateTXClient(func(db *gorm.DB) error {
		return db.Table("record_swipe_times").Limit(1).Updates(&r).Error
	})
}
